using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Hanoi
{
    public class FormLevel : Hanoi.FormOptions
    {
        private ComboBox cbMoves;
        private ComboBox cbSeconds;
        private CheckBox chkSeconds;
        private PictureBox pbClock;
        private Label lblMoves;
        private PictureBox pbFlag;
        private System.ComponentModel.IContainer components = null;

        private Form _callerForm;
        private Form _disksNumberForm;
        private Form _columnsOrderForm;
        private Panel pnControls;
        private Hanoi.Logic.Profiles.CustomProfile _cp;

        public FormLevel(Form caller, Hanoi.Logic.Profiles.CustomProfile cp, Form diskNumberForm, Form columnsOrderForm)
        {
            InitializeComponent();
            base.CenterPanel(pnControls);
            _callerForm = caller;
            _disksNumberForm = diskNumberForm;
            _columnsOrderForm = _columnsOrderForm;
            _cp = cp;

            cbMoves.SelectedIndex = 0;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLevel));
            this.cbMoves = new System.Windows.Forms.ComboBox();
            this.cbSeconds = new System.Windows.Forms.ComboBox();
            this.chkSeconds = new System.Windows.Forms.CheckBox();
            this.pbClock = new System.Windows.Forms.PictureBox();
            this.lblMoves = new System.Windows.Forms.Label();
            this.pbFlag = new System.Windows.Forms.PictureBox();
            this.pnControls = new System.Windows.Forms.Panel();
            // 
            // mnuPrevious
            // 
            this.mnuPrevious.Click += new System.EventHandler(this.mnuPrevious_Click);
            // 
            // mnuNext
            // 
            this.mnuNext.Text = "Start";
            this.mnuNext.Click += new System.EventHandler(this.mnuNext_Click);
            // 
            // pnOptions
            // 
            this.pnOptions.Controls.Add(this.pnControls);
            // 
            // lblInfo
            // 
            this.lblInfo.Font = new System.Drawing.Font("Nina", 8F, System.Drawing.FontStyle.Regular);
            this.lblInfo.Text = "Finally, adjust a pair of settings more to improve your gaming experience. You\'ll" +
                " see a Flag while changing values: it means tough level.";
            // 
            // cbMoves
            // 
            this.cbMoves.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
            this.cbMoves.Items.Add("No Plus");
            this.cbMoves.Items.Add("Plus 25% ");
            this.cbMoves.Items.Add("Plus 50%");
            this.cbMoves.Items.Add("Plus 100%");
            this.cbMoves.Items.Add("Plus 200%");
            this.cbMoves.Location = new System.Drawing.Point(46, 28);
            this.cbMoves.Size = new System.Drawing.Size(99, 23);
            this.cbMoves.SelectedIndexChanged += new System.EventHandler(this.cbMoves_SelectedIndexChanged);
            // 
            // cbSeconds
            // 
            this.cbSeconds.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
            this.cbSeconds.Items.Add("1");
            this.cbSeconds.Items.Add("2");
            this.cbSeconds.Items.Add("5");
            this.cbSeconds.Items.Add("7");
            this.cbSeconds.Items.Add("10");
            this.cbSeconds.Location = new System.Drawing.Point(44, 77);
            this.cbSeconds.Size = new System.Drawing.Size(101, 23);
            this.cbSeconds.SelectedIndexChanged += new System.EventHandler(this.cbSeconds_SelectedIndexChanged);
            // 
            // chkSeconds
            // 
            this.chkSeconds.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
            this.chkSeconds.Location = new System.Drawing.Point(45, 52);
            this.chkSeconds.Size = new System.Drawing.Size(100, 22);
            this.chkSeconds.Text = "Use Timing?";
            this.chkSeconds.CheckStateChanged += new System.EventHandler(this.chkSeconds_CheckStateChanged);
            // 
            // pbClock
            // 
            this.pbClock.Image = ((System.Drawing.Image)(resources.GetObject("pbClock.Image")));
            this.pbClock.Location = new System.Drawing.Point(26, 54);
            this.pbClock.Size = new System.Drawing.Size(16, 16);
            this.pbClock.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            // 
            // lblMoves
            // 
            this.lblMoves.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
            this.lblMoves.Location = new System.Drawing.Point(46, 3);
            this.lblMoves.Size = new System.Drawing.Size(77, 22);
            this.lblMoves.Text = "Moves limit";
            // 
            // pbFlag
            // 
            this.pbFlag.Location = new System.Drawing.Point(26, 3);
            this.pbFlag.Size = new System.Drawing.Size(16, 16);
            this.pbFlag.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            // 
            // pnControls
            // 
            this.pnControls.Controls.Add(this.pbFlag);
            this.pnControls.Controls.Add(this.pbClock);
            this.pnControls.Controls.Add(this.lblMoves);
            this.pnControls.Controls.Add(this.cbMoves);
            this.pnControls.Controls.Add(this.chkSeconds);
            this.pnControls.Controls.Add(this.cbSeconds);
            this.pnControls.Location = new System.Drawing.Point(3, 12);
            this.pnControls.Size = new System.Drawing.Size(171, 103);
            // 
            // FormLevel
            // 
            this.ClientSize = new System.Drawing.Size(176, 180);
            this.Text = "Level";

        }
        #endregion

        #region My Code
        Hanoi.Logic.LevelEvaluation _le = null;

        double[] _movesRates = 
            new double[] { 1, 1.25, 1.5, 2, 3 };
        string[] _levelNames = 
            new string[] { "White heat", "Yellow submarine", "Clockwork Orange", "Red.mond", "Black Ice" };

        private Image GetLevelImage(int levelNumber)
        {
            this.Text = string.Format("Level {0}: {1}", levelNumber, _levelNames[levelNumber]);

            switch (levelNumber)
            {
                case 0:
                    return Hanoi.Properties.Resources.Level0;
                case 1:
                    return Hanoi.Properties.Resources.Level1;
                case 2:
                    return Hanoi.Properties.Resources.Level2;
                case 3:
                    return Hanoi.Properties.Resources.Level3;
                case 4:
                    return Hanoi.Properties.Resources.Level4;
                default:
                    return Hanoi.Properties.Resources.MoveInvalid;
            }
        }

        private void RefreshLevel()
        {
            if (_le == null)
                _le = new Hanoi.Logic.LevelEvaluation(_cp);

            if (cbMoves.SelectedIndex >= 0)
                _cp.Moves = Convert.ToInt32(
                    _cp.CalculateMoves(_cp.Size) * _movesRates[cbMoves.SelectedIndex]);
            else
                _cp.Moves = 1;

            if (cbSeconds.SelectedIndex >= 0 && chkSeconds.Checked)
            {
                int seconds = int.Parse(cbSeconds.SelectedItem.ToString());
                _cp.TimeControled = true;
                _cp.TimeAmount = new TimeSpan(0, 0, _cp.Moves * seconds);
            }
            else
            {
                _cp.TimeControled = false;
                _cp.TimeAmount = new TimeSpan(0, 0, 0);
            }

            pbFlag.Image = GetLevelImage(_le.Level);
        }

        private void Next()
        {
            FormProfiles f = (FormProfiles)_callerForm;
            f.ShowForm(_cp);
            _disksNumberForm.Dispose();
        }
        #endregion

        #region Events
        private void cbMoves_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshLevel();
        }

        private void chkSeconds_CheckStateChanged(object sender, EventArgs e)
        {
            if (chkSeconds.Checked)
                cbSeconds.SelectedIndex = 0;
            else
                cbSeconds.SelectedIndex = -1;
            RefreshLevel();
        }

        private void cbSeconds_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshLevel();
        }

        private void mnuPrevious_Click(object sender, EventArgs e)
        {
            this._callerForm.Show();
            this._callerForm.Visible = true;
            this._callerForm.BringToFront();
            this._callerForm.Focus();

            try
            {
                _disksNumberForm.Dispose();
                _columnsOrderForm.Dispose();
                this.Dispose();
            }
            catch { }
        }

        private void mnuNext_Click(object sender, EventArgs e)
        {
            Next();
            this.Dispose();
        }
        #endregion
    }
}
